package com.sunnyebuy.merchant.wrapper;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpSession;

/**
 * Created by Eclipse Package: com.sunnyebuy.merchant.wrapper Copyright :
 * Copyright (c) 2008 - 2012 All rights reserved. Created : 2012-1-31 下午8:36:13
 * 
 * @author Wenzhong Gu
 * @version 1.0
 */
public class I18nRequestWrapper extends HttpServletRequestWrapper {

	private Locale locale = null;

	public I18nRequestWrapper(HttpServletRequest request) {
		super(request);
		HttpSession session = request.getSession();
		locale = (Locale) session.getAttribute("WW_TRANS_I18N_LOCALE");
	}

	/**
	 * struts2的BUG，如果重定向的话，国际化默认取HTTP请求头中的参数 替换HTTP请求参数
	 **/
	@Override
	public String getHeader(String name) {
		String value = super.getHeader(name);
		if ("Accept-Language".equals(name) && locale != null) {
			value = locale.getLanguage() + "_" + locale.getCountry()
					+ value.substring(6, value.length());
		}
		return value;
	}

	@Override
	public Locale getLocale() {
		if (locale != null) {
			return locale;
		}
		return super.getLocale();
	}

}